package com.example.hello.service.impl;

import com.example.hello.common.PageResult;
import com.example.hello.entity.Dept;
import com.example.hello.mapper.DeptMapper;
import com.example.hello.service.DeptService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DeptServiceImpl implements DeptService {

    @Autowired
    private DeptMapper deptMapper;

    @Override
    public List<Dept> list() {
        return deptMapper.list();
    }

    @Override
    public PageResult page(String name, Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        List<Dept> deptList;
        if (name != null && !name.trim().isEmpty()) {
            deptList = deptMapper.listByName(name);
        } else {
            deptList = deptMapper.list();
        }
        Page<Dept> p = (Page<Dept>) deptList;
        return new PageResult(p.getTotal(), p.getResult());
    }

    @Override
    public Dept getById(Integer id) {
        return deptMapper.getById(id);
    }

    @Override
    public void add(Dept dept) {
        deptMapper.insert(dept);
    }

    @Override
    public void update(Dept dept) {
        deptMapper.update(dept);
    }

    @Override
    public void deleteById(Integer id) {
        deptMapper.deleteById(id);
    }
} 